#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
int *arr=NULL;
int size=0;
append(int elem)
{
arr=(int *)realloc(arr,(size+1)*sizeof(int));
arr[size]=elem;
size = size + 1;					
}
display()
{
	int i;
	for(i=0;i<size;i++)
		printf("%d ,",arr[i]);
}
count(int elem)
{
	int c=0,i;
	for(i=0;i<size;i++)
	{
		if(arr[i]==elem)
			c++;
	}
	printf("The number of times %d appears in the list is : %d",elem,c);
}
removeitem(int elem)
{
	/*int i,pos=0;
	for(i=0;i<size;i++)
	{
		if(arr[i]==elem)
			{pos=i;
			//arr=(int *)realloc(arr,(size-1)*sizeof(int));
			break;}
	}
	for(i=pos;pos<size;pos++)
	{
		arr[pos]=arr[pos+1];
		size=size-1;
		
	}*/
	int i=0,count=0,j;
	for (i=0; i<size; i++)
	{if (arr[i]==elem)
		{count=1; 
				 
		arr=realloc(arr,(size-1)*sizeof(int));
		/*if(arr==NULL)
{printf("exiting"); exit(0);}*/
		break;}}
 	for(j=size; j>i;j--)
		{arr[j]=arr[j-1];}
	
	

	if(count==0)
	{printf("number not in the list\n");}
	else{for (i=0; i<size-1;i++)
		{printf(" %d  ", arr[i]);}}
} 
	
main()
{
int y=1;
arr=(int *)malloc(size*sizeof(int));
do
{ 	system("clear");
	int ch;
	printf("\n1.Display.\n2.list.append(x).\n3. Count\n4.Remove.\n5.Index\n11.Exit.\nEnter your choice :  ");
	scanf("%d",&ch);
	switch(ch)
	{
		case 1 :
			{
			printf("\nThe is list is : ");
			display();
			printf("\nIf you want to continue enter 1, else press any other number : ");
			scanf("%d",&y);
			break;}

		case 2 :
			{
			int ele;
			printf("Enter the element you want to append to the list : ");
			scanf("%d",&ele);
			append(ele);
			printf("\nIf you want to continue enter 1, else press any other number : ");
			scanf("%d",&y);		
			break;}
		
		case 3 :{
				int ele;
				printf("\nEnter the element you want to count the occurences of : ");
				scanf("%d",&ele);
				count(ele);
				printf("\nIf you want to continue enter 1, else press any other number : ");
				scanf("%d",&y);		
				break;}			

		case 4 :
			{	
				int ele;
				printf("\nEnter the element you want to remove the first occurence of : ");
				scanf("%d",&ele);
				removeitem(ele);				
				printf("\nIf you want to continue enter 1, else press any other number : ");
				scanf("%d",&y);
				break;}
			
		
		case 5 :
			{
				int p,i;
				printf("Enter the element you want the index of :");
				scanf("%d",&p);
				for(i=0;i<size;i++)
				{
					if(arr[i]==p)
						count=p;
					if(count!=0)
						break;
				}
				printf("The index is : ",count);		
				printf("\nIf you want to continue enter 1, else press any other number : ");
				scanf("%d",&y);		
				break;}
		case 6 :{
				
				printf("\nIf you want to continue enter 1, else press any other number : ");
				scanf("%d",&y);		
				break;}
		case 7 :{
				
				printf("\nIf you want to continue enter 1, else press any other number : ");
				scanf("%d",&y);		
				break;}
		case 8 :{
				
				printf("\nIf you want to continue enter 1, else press any other number : ");
				scanf("%d",&y);		
				break;}
		case 9 :{
				
				printf("\nIf you want to continue enter 1, else press any other number : ");
				scanf("%d",&y);		
				break;}
		case 10 :{
				
				printf("\nIf you want to continue enter 1, else press any other number : ");
				scanf("%d",&y);		
				break;}
										
		case 11 :			
  			system("clear");
			exit(0);	
		default : 
			{	printf("\nILLEGAL CHOICE");
				printf("\nIf you want to continue enter 1, else press any other number : ");
				scanf("%d",&y);		
				break;}
			
		        
	}
}while(y==1);	
	return 0;
}
